package com.microservices.yucloud.mqcenter.controller;

import com.microservices.yucloud.mqcenter.rocket.ChannelSource;
import com.microservices.yucloud.mqcenter.rocket.Foo;
import com.microservices.yucloud.mqcenter.service.SenderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zy
 * @date 2019/10/12 5:28 下午
 **/
@Slf4j
@RequestMapping("rocket")
@RestController
public class RocketController {

    @Autowired
    private SenderService senderService;

    @Autowired
    private ChannelSource source;

    @GetMapping("/test")
    public Object test(String bindingName) {
        if (bindingName.equals("output1")) {
            int count = 5;
            for (int index = 1; index <= count; index++) {
                String msgContent = "msg-" + index;
                if (index % 3 == 0) {
                    senderService.send(msgContent);
                } else if (index % 3 == 1) {
                    senderService.sendWithTags(msgContent, "tagStr");
                } else {
                    senderService.sendObject(new Foo(index, "foo"), "tagObj");
                }
            }
        } else if (bindingName.equals("output3")) {
            int count = 20;
            for (int index = 1; index <= count; index++) {
                String msgContent = "pullMsg-" + index;
                source.output3().send(MessageBuilder.withPayload(msgContent).build());
            }
        }
        return "ok";
    }

    @GetMapping("/test/delay")
    public Object testDelay() {
        senderService.sendObjectDelay(new Foo(1, "foo"), "tagObj", 3);
        return "ok";
    }

    @GetMapping("/test/transactional")
    public Object testTransactional() {
        // COMMIT_MESSAGE message
        senderService.sendTransactionalMsg("transactional-msg1", 1);
        // ROLLBACK_MESSAGE message
        senderService.sendTransactionalMsg("transactional-msg2", 2);
        // ROLLBACK_MESSAGE message
        senderService.sendTransactionalMsg("transactional-msg3", 3);
        // COMMIT_MESSAGE message
        senderService.sendTransactionalMsg("transactional-msg4", 4);
        return "ok";
    }

}
